package com.eclink.iot.rfid;

import lombok.Data;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@Data
public class RFIDDevice {

    /**
     * 设备的状态
     */
    private int status = 0;

    /**
     * 设备的UUID
     */
    private String uuid;

    /**
     * 上次激活时间
     */
    private Date lastActiveTime;


    private Set<RFIDDeviceClient> clientList = new HashSet<>();

    public void setStatus(int status) {
        if (status != this.status) {
           try {
               RFIDServer.on_device_online_status_change(this.uuid, status);
           }catch(Exception e) {
               log.error("some error happen while execute status listener", e);
           }
        }
        this.status = status;
    }

    public void addClient(RFIDDeviceClient client) {
        clientList.add(client);
    }

    public void removeClient(RFIDDeviceClient client) {
        clientList.remove(client);
    }

    /**
     * 检测设备的激活状态
     */
    public void checkActiveStatus() {

        if (lastActiveTime == null) {
            status = 0;
            return;
        }

        long now = System.currentTimeMillis();
        long diff = now - lastActiveTime.getTime();
        // 判断上次激活时间是否已经超过了
        if (diff < 20000) {
            status = 1;
        } else {
            status = 0;
        }
    }

}
